import { Col, Row, Switch, Button, Tag, Field, RadioGroup, Radio,Stepper } from "vant";
import SendInstruct from "@/mixin/SendInstruct.js";
import { findComponentUpward } from "@/tools/index.js";
export default {
  name: "AirCd",
  mixins: [SendInstruct],
  props: {
    // 设备对象
    obj: {
      type: Object,
      default: {},
    },
  },
  watch: {
    obj: {
      handler(newVal) {
        console.info("obj::" + JSON.stringify(newVal));
        this.deviceObj.fileName = newVal.fileName;
        this.deviceObj.equipmentId = newVal.equipmentId;
        this.deviceObj.equipmentName = newVal.equipmentName;
        this.deviceObj.onOffText = newVal.onOffText ? newVal.onOffText : "";
        this.deviceObj.onOffType = newVal.onOffType ? newVal.onOffType : "";
        this.deviceObj.equipmentStatusText = newVal.equipmentStatusText
          ? newVal.equipmentStatusText
          : "";
        this.deviceObj.equipmentStatusType = newVal.equipmentStatusType
          ? newVal.equipmentStatusType
          : "";
        this.deviceObj.parameterArr = newVal.parameterArr
          ? newVal.parameterArr
          : [];
        if (this.deviceObj.parameterArr.length > 0) {
          this.deviceObj.parameterArr.forEach((item) => {
            console.info(JSON.stringify(item));
            if (item.parameterName == "mode") {
              this.mode = item.defaultValue;
            }
            if (item.parameterName == "temperature") {
              this.temperature = item.defaultValue;
            }
            if (item.parameterName == "wind") {
              this.wind = item.defaultValue+'';
            }
          });
        }
      },
      immediate: true,
      deep: true,
    },
  },
  data() {
    return {
      // 设备对象
      deviceObj: {
        // 图片路径
        fileName: "",
        // 设备id
        equipmentId: "",
        // 设备名称
        equipmentName: "",
        // 开关名称
        onOffText: "",
        // 类型
        onOffType: "",
        // 设备状态文字
        equipmentStatusText: "",
        // 设备状态类型
        equipmentStatusType: "",
        // 设备参数
        parameterArr: [],
      },
      // 模式
      mode: "cool",
      // 温度
      temperature: 0,
      // 风速
      wind: 0,
    };
  },
  components: {
    [Col.name]: Col,
    [Row.name]: Row,
    [Switch.name]: Switch,
    [Button.name]: Button,
    [Tag.name]: Tag,
    [Field.name]: Field,
    [RadioGroup.name]: RadioGroup,
    [Radio.name]: Radio,
    [Stepper.name]: Stepper
  },
  created() {},
  mounted() {},
  computed: {},
  methods: {
    // 获取状态
    getState() {
      this.lot_equipmentControl_control_api({
        equipmentId: this.deviceObj.equipmentId,
        cmd: "INF_COMMON_getDevStatus",
        parameter: [],
      });
    },
    // 设置模式
    setMode(val) {
      this.mode = val;
      this.lot_equipmentControl_control_api({
        equipmentId: this.deviceObj.equipmentId,
        cmd: "CMD_AIR_CD_set",
        parameter: [
          {
            name: "mode",
            value: this.mode,
          },
          {
            name: "temperature",
            value: this.temperature,
          },
          {
            name: "wind",
            value: this.wind,
          },
        ],
      });
      this.setWebsocket({
        websocketType: "equipmentControl",
        equipmentId: this.deviceObj.equipmentId,
      });
    },
    // 设置温度
    setTemperature(val) {
      this.temperature = val;
      this.lot_equipmentControl_control_api({
        equipmentId: this.deviceObj.equipmentId,
        cmd: "CMD_AIR_CD_set",
        parameter: [
          {
            name: "mode",
            value: this.mode,
          },
          {
            name: "temperature",
            value: this.temperature,
          },
          {
            name: "wind",
            value: this.wind,
          },
        ],
      });
      this.setWebsocket({
        websocketType: "equipmentControl",
        equipmentId: this.deviceObj.equipmentId,
      });
    },
    // 设置风速
    setwind(val) {
      this.wind = val;
      this.lot_equipmentControl_control_api({
        equipmentId: this.deviceObj.equipmentId,
        cmd: "CMD_AIR_CD_set",
        parameter: [
          {
            name: "mode",
            value: this.mode,
          },
          {
            name: "temperature",
            value: this.temperature,
          },
          {
            name: "wind",
            value: this.wind,
          },
        ],
      });
      this.setWebsocket({
        websocketType: "equipmentControl",
        equipmentId: this.deviceObj.equipmentId,
      });
    },
    // 关闭空调
    getStop() {
      this.lot_equipmentControl_control_api({
        equipmentId: this.deviceObj.equipmentId,
        cmd: "CMD_AIR_CD_off",
        parameter: [],
      });
      this.setWebsocket({
        websocketType: "equipmentControl",
        equipmentId: this.deviceObj.equipmentId,
      });
    },
    // 刷新
    refreshFn() {
      let obj = findComponentUpward(this, "deviceList");
      obj.lot_equipmentControl_status_api(this.deviceObj.equipmentId, "reload");
    },
  },
};
